const commentService = require("../service/comment.service");

class CommentController {
  // 创建
  async create(ctx, next) {
    const { momentId, content } = ctx.request.body;
    const { id } = ctx.user;
    const result = await commentService.create(momentId, content, id);
    ctx.body = {
      data: result,
      code: 200,
      message: "创建成功",
    };
  }

  //回复评论
  async reply(ctx, next) {
    const { commentId } = ctx.params;
    const { momentId, content } = ctx.request.body;
    if (!momentId || !commentId) {
      return (ctx.body = {
        code: 500,
        message: "请传入momentId或者commentId",
      });
    }

    const { id } = ctx.user;
    const result = await commentService.reply(momentId, content, id, commentId);
    ctx.body = { data: result, code: 200, message: "发表评论成功" };
  }

  //更新评论
  async update(ctx, next) {
    const { commentId } = ctx.params;
    const { content } = ctx.request.body;
    const result = await commentService.update(commentId, content);
    ctx.body = { data: result, code: 200, message: "修改成功" };
  }

  //删除评论
  async remove(ctx, next) {
    try {
      const { commentId } = ctx.params;
      const result = await commentService.remove(commentId);
      ctx.body = { data: result, code: 200, message: "删除成功" };
    } catch (error) {
      console.log(error);
    }
  }
}

module.exports = new CommentController();
